package com.yuhanginfo.modules.web.web;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import com.yuhanginfo.common.web.BaseController;
import com.yuhanginfo.modules.web.util.WebUserUtils;
import com.yuhanginfo.modules.web.entity.WebReturnsGoods;
import com.yuhanginfo.modules.web.entity.WebYongUser;
import com.yuhanginfo.modules.web.service.WebExchangeHistoryService;

/**
 * 退换货
 * @author yinhao
 * @version 2017-11-21
 */
@Controller
@RequestMapping(value = "${frontPath}/exchangeHistory")
public class WebExchangeHistoryController extends BaseController {

	@Autowired
	private WebExchangeHistoryService webExchangeHistoryService;
	
	

	@RequiresPermissions("user")
	@RequestMapping(value = "")
	public String list(WebReturnsGoods webReturnsGoods, HttpServletRequest request, HttpServletResponse response, Model model) {
		WebYongUser us = WebUserUtils.getYongUser();
		webReturnsGoods.setUserId(us.getId());
		try{
		if(null!=webReturnsGoods.getAllDate() && !"".equals(webReturnsGoods.getAllDate())){
			SimpleDateFormat s = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
			Date starDate = s.parse(webReturnsGoods.getAllDate().substring(0,10)+" 00:00:00");
			Date endDate = s.parse(webReturnsGoods.getAllDate().substring(13,23)+" 23:59:59");
			webReturnsGoods.setStartTime(starDate);
			webReturnsGoods.setEndTime(endDate);
		}
		}catch(Exception ex){
			logger.error(ex.getMessage(),ex);
		}
		List<WebReturnsGoods> list = webExchangeHistoryService.findAll(webReturnsGoods); 
		model.addAttribute("actionCenter", "exchangeHistory");
		model.addAttribute("list", list);
		return "modules/web/exchangeHistoryList";
	}

	@RequiresPermissions("user")
	@RequestMapping(value = "form")
	public String form(WebReturnsGoods returnsGoods,Model model) {
		try{
			model.addAttribute("actionCenter", "exchangeHistory");
			model.addAllAttributes(webExchangeHistoryService.findDetail(returnsGoods));
		}catch(Exception ex){
			logger.error(ex.getMessage(),ex);
		}
		return "modules/web/exchangeHistoryDetailList";
	}
	
}